'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceAttendanceDetailCtrl
 * @description
 * # AttendanceAttendanceDetailCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AttendanceAttendanceDetailCtrl', function ($location, DataService, $rootScope, openApi, $routeParams, Util, localStorageService) {
        var vm = this;
        this.pkId = $routeParams.pkId || "";
        var weeks = [
            { value: 1, key: "星期一" },
            { value: 2, key: "星期二" },
            { value: 3, key: "星期三" },
            { value: 4, key: "星期四" },
            { value: 5, key: "星期五" },
            { value: 6, key: "星期六" },
            { value: 7, key: "星期天" }
        ];
        this.offsets = [300, 500, 800, 1000];
        vm.sheetObj = {};
        // 底部关联模块类型选框添加
        var attDays = {};
        attDays.sheettitle = "考勤时间";
        attDays.callback = function (days) {
            vm.param.dutyDays = days.map(function (item, index) {
                return item.value;
            })
            vm.param.cycleTime = days.map(function (item, index) {
                return item.key;
            }).toString();
        }
        vm.sheetObj.attDays = attDays;
        vm.showrelModule = function () {
            vm.sheetObj.attDays.tabList = weeks
            vm.showSheet("attDays")
        };
        vm.showSheet = function (type) {
            var sheetObj = vm.sheetObj[type]
            vm.sheetObj.type = type
            vm.sheetObj.tabList = sheetObj.tabList
            vm.sheetObj.sheettitle = sheetObj.sheettitle
            vm.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        // 提交考勤
        this.submit = function () {
            _getTime();
            if (!vm.startTime) {
                Util.Toast("请选择上班时间");
                return;
            }
            if (!vm.endTime) {
                Util.Toast("请选择下班时间");
                return;
            }
            if (!vm.param.cycleTime) {
                Util.Toast("请选择考勤日期");
                return;
            }
            if (!checkTime()) {
                Util.Toast("下班时间要大于上班时间");
                return;
            }
            if (!vm.param.department) {
                Util.Toast("请选择考勤部门");
                return;
            }
            if (!vm.param.address) {
                Util.Toast("请选择考勤地点");
                return;
            }
            console.log(vm.param);
            DataService.getData(openApi.attAdd, vm.param).then(function (data) {
                Util.Toast(data, { callback: Util.goBack })
            })
        }
        function checkTime() {
            var onduty = parseInt(vm.param.onDutyHour) * 60 + parseInt(vm.param.onDutyMintue);
            var offduty = parseInt(vm.param.offDutyHour) * 60 + parseInt(vm.param.offDutyMintue);
            return offduty > onduty
        }
        this.showusers = function () {
            _getTime();
            localStorageService.set("attDetail", vm.param);
            $location.path("attendance/attendance_user/" + vm.pkId)
        }
        this.showdeps = function () {
            _getTime();
            localStorageService.set("attDetail", vm.param);
            $location.path("attendance/attendance_deps/" + vm.pkId)
        };
        this.showMap = function () {
            _getTime();
            localStorageService.set("attDetail", vm.param);
            $location.path("attendance/edit_location")
        };
        this.confirmdel = function () {
            if (navigator.notification) {
                navigator.notification.confirm(
                    '是否删除该考勤，删除后所有考勤记录将被清除',
                    vm.checkConfirm(),
                    '删除考勤', ['删除', '取消']
                )
            } else {
                this.checkConfirm()(1)
            }
        }
        this.checkConfirm = function () {
            return function (btn) {
                if (btn == 1) {
                    var param = {}
                    param.pkId = vm.pkId;
                    DataService.getData(openApi.attDel, param).then(function (data) {
                        Util.Toast(data, { callback: Util.goBack })
                    })
                }
            }
        }
        var init = function () {
            var options = {};
            options.pkId = vm.pkId;
            vm.param = localStorageService.get("attDetail");
            // 获取后马上清除
            localStorageService.remove("attDetail");
            // 如果有缓存用缓存
            // 没有缓存则判断如果是新建则设置新对象否则请求接口数据
            if (vm.param) {
                _setTime()
            } else {
                if (options.pkId) {
                    DataService.getData(openApi.attGet, options).then(function (data) {
                        vm.param = data;
                        console.log(data)
                        // 初始化默认偏移
                        vm.param.allowableOffset = vm.param.allowableOffset || vm.offsets[0]
                        _setTime();
                    })
                } else {
                    vm.param = {};
                    vm.param.allowableOffset = vm.param.allowableOffset || vm.offsets[0]
                }
            }
        }
        function _setTime() {
            vm.startTime = timeFormat(vm.param.onDutyHour, ":") + timeFormat(vm.param.onDutyMintue)
            vm.endTime = timeFormat(vm.param.offDutyHour, ":") + timeFormat(vm.param.offDutyMintue)
        }
        function timeFormat(time, format) {
            var date = "";
            if (time || time === 0) {
                if (time < 10 && time >= 0) {
                    date = "0" + time
                } else {
                    date = date + time;
                }
                if (format) {
                    date = date + format
                }
            }

            return date;
        }
        function _getTime() {
            if (vm.startTime) {
                vm.param.onDutyHour = parseInt(vm.startTime.split(":")[0])
                vm.param.onDutyMintue = parseInt(vm.startTime.split(":")[1]);
            }
            if (vm.endTime) {
                vm.param.offDutyHour = parseInt(vm.endTime.split(":")[0])
                vm.param.offDutyMintue = parseInt(vm.endTime.split(":")[1]);
            }
        }
        init()
    });
